using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Configuration;

namespace Developex.DatabaseWrapper
{
	/// <summary>
	/// Default conn creator
	/// </summary>
	public class DefaultConnectionCreator
	{
		/// <summary>
		/// Connection name in web.config
		/// </summary>
		public string ConnectionName { get; set; }

		/// <summary>
		/// Constructor. Get conn string from web config by name.
		/// </summary>
		/// <param name="connectionName"></param>
		public DefaultConnectionCreator(string connectionName)
		{
			ConnectionName = connectionName;
		}

		/// <summary>
		/// Constructor. Get last conn string from web config conn strings.
		/// </summary>
		public DefaultConnectionCreator()
		{
		}

		/// <summary>
		/// Create object
		/// </summary>
		/// <returns></returns>
		public Connection Create()
		{
			if (string.IsNullOrEmpty(ConnectionName))
				return new Connection(WebConfigurationManager.ConnectionStrings[WebConfigurationManager.ConnectionStrings.Count - 1].ConnectionString);
			return new Connection(WebConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
		}
	}
}
